找传奇、传世资源到传世资源站!

android 图片压缩示例源码

8.5玩家评分(1人评分)
下载后可评
介绍 评论 失效链接反馈

from clipboardpackage id.zelory.compressor.sample;import android.content.Intent;import android.graphics.Bitmap;import android.graphics.BitmapFactory;import android.graphics.Color;import android.os.Bundle;import android.os.Environment;import android.support.v7.app.AppCompatActivity;import android.util.Log;import android.view.View;import android.widget.ImageView;import android.widget.TextView;import android.widget.Toast;import java.io.File;import java.io.IOException;import java.text.DecimalFormat;import java.util.Random;import id.zelory.compressor.Compressor;import io.reactivex.android.schedulers.AndroidSchedulers;import io.reactivex.functions.Consumer;import io.reactivex.schedulers.Schedulers;public class MainActivity extends AppCompatActivity { private static final int PICK_IMAGE_REQUEST = 1; private ImageView actualImageView; private ImageView compressedImageView; private TextView actualSizeTextView; private TextView compressedSizeTextView; private File actualImage; private File compressedImage; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); actualImageView = (ImageView) findViewById(R.id.actual_image); compressedImageView = (ImageView) findViewById(R.id.compressed_image); actualSizeTextView = (TextView) findViewById(R.id.actual_size); compressedSizeTextView = (TextView) findViewById(R.id.compressed_size); actualImageView.setBackgroundColor(getRandomColor()); clearImage(); } public void chooseImage(View view) { Intent intent = new Intent(Intent.ACTION_GET_CONTENT); intent.setType("image/*"); startActivityForResult(intent, PICK_IMAGE_REQUEST); } public void compressImage(View view) { if (actualImage == null) { showError("Please choose an image!"); } else { // Compress image in main thread //compressedImage = new Compressor(this).compressToFile(actualImage); //setCompressedImage(); // Compress image to bitmap in main thread //compressedImageView.setImageBitmap(new Compressor(this).compressToBitmap(actualImage)); // Compress image using RxJava in background thread new Compressor(this) .compressToFileAsFlowable(actualImage) .subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()) .subscribe(new Consumer<File>() { @Override public void accept(File file) { compressedImage = file; setCompressedImage(); } }, new Consumer<Throwable>() { @Override public void accept(Throwable throwable) { throwable.printStackTrace(); showError(throwable.getMessage()); } }); } } public void customCompressImage(View view) { if (actualImage == null) { showError("Please choose an image!"); } else { // Compress image in main thread using custom Compressor try { compressedImage = new Compressor(this) .setMaxWidth(640) .setMaxHeight(480) .setQuality(75) .setCompressFormat(Bitmap.CompressFormat.WEBP) .setDestinationDirectoryPath(Environment.getExternalStoragePublicDirectory( Environment.DIRECTORY_PICTURES).getAbsolutePath()) .compressToFile(actualImage); setCompressedImage(); } catch (IOException e) { e.printStackTrace(); showError(e.getMessage()); } // Compress image using RxJava in background thread with custom Compressor /*new Compressor(this) .setMaxWidth(640) .setMaxHeight(480) .setQuality(75) .setCompressFormat(Bitmap.CompressFormat.WEBP) .setDestinationDirectoryPath(Environment.getExternalStoragePublicDirectory( Environment.DIRECTORY_PICTURES).getAbsolutePath()) .compressToFileAsFlowable(actualImage) .subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()) .subscribe(new Consumer<File>() { @Override public void accept(File file) { compressedImage = file; setCompressedImage(); } }, new Consumer<Throwable>() { @Override public void accept(Throwable throwable) { throwable.printStackTrace(); showError(throwable.getMessage()); } });*/ } } private void setCompressedImage() { compressedImageView.setImageBitmap(BitmapFactory.decodeFile(compressedImage.getAbsolutePath())); compressedSizeTextView.setText(String.format("Size : %s", getReadableFileSize(compressedImage.length()))); Toast.makeText(this, "Compressed image save in " compressedImage.getPath(), Toast.LENGTH_LONG).show(); Log.d("Compressor", "Compressed image save in " compressedImage.getPath()); } private void clearImage() { actualImageView.setBackgroundColor(getRandomColor()); compressedImageView.setImageDrawable(null); compressedImageView.setBackgroundColor(getRandomColor()); compressedSizeTextView.setText("Size : -"); } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); if (requestCode == PICK_IMAGE_REQUEST && resultCode == RESULT_OK) { if (data == null) { showError("Failed to open picture!"); return; } try { actualImage = FileUtil.from(this, data.getData()); actualImageView.setImageBitmap(BitmapFactory.decodeFile(actualImage.getAbsolutePath())); actualSizeTextView.setText(String.format("Size : %s", getReadableFileSize(actualImage.length()))); clearImage(); } catch (IOException e) { showError("Failed to read picture data!"); e.printStackTrace(); } } } public void showError(String errorMessage) { Toast.makeText(this, errorMessage, Toast.LENGTH_SHORT).show(); } private int getRandomColor() { Random rand = new Random(); return Color.argb(100, rand.nextInt(256), rand.nextInt(256), rand.nextInt(256)); } public String getReadableFileSize(long size) { if (size <= 0) { return "0"; } final String[] units = new String[]{"B", "KB", "MB", "GB", "TB"}; int digitGroups = (int) (Math.log10(size) / Math.log10(1024)); return new DecimalFormat("#,##0.#").format(size / Math.pow(1024, digitGroups)) " " units[digitGroups]; }}

评论

发表评论必须先登陆, 您可以 登陆 或者 注册新账号 !


在线咨询: 问题反馈
客服QQ:174666394

有问题请留言,看到后及时答复